package dao;

import util.DBHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserOrdersDao {

    public List<Map<String, Object>> selectUserOrders(String uid, String proname, String status){

        String sql = "SELECT p.image, proname, o.orderid, o.`status`, p.price, d.currentprice, d.quantity, detailid " +
                "FROM shopping_order o inner JOIN orderdetail d ON o.orderid=d.orderid " +
                "INNER JOIN shopping_product p ON d.proid = p.proid where uid = ? ";

        ArrayList<Object> params = new ArrayList<>();
        params.add(uid);
        if(proname !=null && proname.trim().isEmpty() == false){
            sql += " and proname like ? ";
            params.add("%" + proname + "%");
        }

        if(status != null && status.trim().isEmpty() == false){
            sql += " and d.`status` = ? ";
            params.add(status);
        }

        return DBHelper.selectListMap(sql, params.toArray());
    }

    // 用户订单 删除（大）
    public int removeOrder(String orderid){
        String sql = "delete FROM shopping_order where orderid = ? ";
        return DBHelper.update(sql, orderid);
    }

    // 用户订单 删除（小）
    public int removeOrderdetail(String detailid){
        String sql = "delete FROM orderdetail where detailid = ? ";
        return DBHelper.update(sql, detailid);
    }

    // 用户订单 退货（小）
    public int rejectedMin(String detailid){
        String sql = "update orderdetail set status = 10 where detailid = ? ";
        return DBHelper.update(sql, detailid);
    }

    // 用户订单 退货（大）
    public int rejectedMax(String orderid){
        String sql = "update shopping_order set status = 0 where orderid = ? ";
        return DBHelper.update(sql, orderid);
    }
}
